//
//  MyRedPacketViewController.m
//  ChengHuiTong
//
//  Created by ypwl on 16/8/5.
//  Copyright © 2016年 张文权. All rights reserved.
//

#import "MyRedPacketViewController.h"
#import "YPRedPacketTableViewCell.h"
#import "SelectView.h"
@interface MyRedPacketViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation MyRedPacketViewController{
    
    UIButton*_canUseButton,*_UnUseButton,*_usedButton;
    
    NSMutableArray*_canUseArr,*_UnUseArr,*_usedArr;
    
    UITableView*_canUseTableView,*_UnUseTableView,*_usedTableView;
    
    UIScrollView*_backGroundScrollView;
    
    UIView*_slideLineView;
    
    BOOL twoPage,threePage;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title=@"我的红包";
    
    self.view.backgroundColor=[UIColor whiteColor];
    
    _canUseArr=[NSMutableArray array];
    
     _usedArr=[NSMutableArray array];
    
     _UnUseArr=[NSMutableArray array];
    
    [self menuView];
    
    [self creatTableView];
    
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
}

-(void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    
}

-(void)getMyRedBag{
    
    YPredBagBin*bin=[[YPredBagBin alloc] init];
    
    bin.token=[YPShareAccountInfo shareAccountInfo].token;
    
    
    
    [YPHttpRequestMethod sendAsyRequestURL:getRedBag withData:[bin dictionaryValue] withCompletionBlock:^(NSDictionary *responseDictionary) {
        
        
        [_canUseTableView.header endRefreshing];
        
        [_usedTableView.header endRefreshing];
        
        [_UnUseTableView.header endRefreshing];
        
        if ([responseDictionary[@"status"] intValue]==0) {
            
            
            [_canUseArr removeAllObjects];
            
            [_usedArr removeAllObjects];
            
            [_UnUseArr removeAllObjects];
            
            NSArray*listArr=responseDictionary[@"list"];
            
            for (NSDictionary*dic in listArr) {
                
                RedbagModel*model=[[RedbagModel alloc] initWithDic:dic];
                
                if ([model.status intValue]==1) {
                    
                    [_canUseArr addObject:model];
                    
                }else if ([model.status intValue]==2){
                    
                    [_usedArr addObject:model];
                    
                }else if ([model.status intValue]==3){
                    
                    [_UnUseArr addObject:model];
                    
                }
                    
                    
                
            }
            
            if (_canUseArr.count>0) {
                
                _canUseTableView.tableFooterView=[[UIView alloc] init];
                
            }else{
                _canUseTableView.tableFooterView=[SelectView footViewWithheight:_canUseTableView.height];
            }
            if (_usedArr.count>0) {
                
                _usedTableView.tableFooterView=[[UIView alloc] init];
                
            }else{
                _usedTableView.tableFooterView=[SelectView footViewWithheight:_usedTableView.height];
            }
            if (_UnUseArr.count>0) {
                
                _UnUseTableView.tableFooterView=[[UIView alloc] init];
                
            }else{
                _UnUseTableView.tableFooterView=[SelectView footViewWithheight:_UnUseTableView.height];
            }
            
            [_canUseTableView reloadData];
            
            [_usedTableView reloadData];
            
            [_UnUseTableView reloadData];
            
        }
        
    } withFailedBlock:^(NSError *error) {
        
    } withIndicator:NO atView:self.view];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView==_canUseTableView) {
    
        return _canUseArr.count;
        
    }else if (tableView==_usedTableView){
        
     
        return _usedArr.count;
        
    }else if (tableView==_UnUseTableView){
        
        
        return _UnUseArr.count;
    }
    
    
    return 0;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YPRedPacketTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (!cell) {
        
        
        cell=[[YPRedPacketTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    if (tableView==_canUseTableView) {
        
        cell.model=_canUseArr[indexPath.row];
        
    }else if (tableView==_usedTableView){
        
        
      cell.model= _usedArr[indexPath.row];
        
    }else if (tableView==_UnUseTableView){
        
        
        cell.model= _UnUseArr[indexPath.row];
    }

    
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    return 15+(SCREEN_WIDTH-20)*0.4;
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    for (int i=100; i<103; i++) {
        
        UIButton*button=(UIButton*)[self.view viewWithTag:i];
        
        if ([NSString stringWithFormat:@"%f",button.left].integerValue==[NSString stringWithFormat:@"%f",_slideLineView.left].integerValue) {
            
            [button setTitleColor:kColor_AppNavColor forState:0];
            
            switch (button.tag) {
                case 101:{
                    
                    if (twoPage) {
                        
                        twoPage=NO;
                        
                        [_usedTableView.header beginRefreshing];
                        
                    }
                    
                    
                }
                    
                    break;
                case 102:{
                    if (threePage) {
                        
                        threePage=NO;
                        [_UnUseTableView.header beginRefreshing];
                    }
                    
                }
                    
                    break;
                    
                default:
                    break;
            }
            
            
        }else{
            
            [button setTitleColor:UIColorFromString(@"#b5b5b5") forState:0];
            
            
            
        }
        
        
        
    }
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (scrollView==_backGroundScrollView){
        
        float offSize=_backGroundScrollView.contentOffset.x/SCREEN_WIDTH;
        
         _slideLineView.left=offSize* SCREEN_WIDTH/3.0;
   
    }
    
    
    
}

#pragma mark  事件响应
- (void)ButtonClick:(UIButton*)button{
    
    
    
    [UIView transitionWithView:self.view duration:0.25 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        _slideLineView.left=button.left;
        
        _backGroundScrollView.contentOffset=CGPointMake(button.left*3.0, 0);
        
        
        
        
        
        
    } completion:^(BOOL finished) {
        
        
        
        
        for (int i=100; i<103; i++) {
            
            UIButton*button1=(UIButton*)[self.view viewWithTag:i];
            
            if (button.tag!=button1.tag) {
                
                [button1 setTitleColor:UIColorFromString(@"#b5b5b5") forState:0];
                
            }else{
                
                
                
                [button1 setTitleColor:kColor_AppNavColor forState:0];
                
                switch (button.tag) {
                    case 101:{
                        
                        if (twoPage) {
                            
                            twoPage=NO;
                            
                            [_usedTableView.header beginRefreshing];
                            
                        }
                        
                        
                    }
                        
                        break;
                    case 102:{
                        if (threePage) {
                            
                            threePage=NO;
                            [_UnUseTableView.header beginRefreshing];
                        }
                        
                    }
                        
                        break;
                        
                        
                        break;
                        
                        
                    default:
                        break;
                }
                
                
                
            }
            
            
            
        }
        
        
        
    }];
    
    
}


-(void)menuView{
    
    _canUseButton=[UIButton buttonWithType:UIButtonTypeCustom];
    
    _canUseButton.frame=CGRectMake(0, 0, SCREEN_WIDTH/3.0, 40);
    
    _canUseButton.tag=100;
    
    [_canUseButton setTitle:@"可用红包" forState:UIControlStateNormal];
    
    [_canUseButton setTitleColor:kColor_AppNavColor forState:UIControlStateNormal];
    
    [_canUseButton addTarget:self action:@selector(ButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    _canUseButton.titleLabel.font=[UIFont systemFontOfSize:15];
    
    [self.view addSubview:_canUseButton];
    
    
    UIView*lineView1=[[UIView alloc] initWithFrame:CGRectMake(_canUseButton.right-0.5, _canUseButton.top+3, 1, _canUseButton.height-6)];
    
    lineView1.backgroundColor=UIColorFromString(@"#b5b5b5");
    
    [self.view addSubview:lineView1];
    
    
    
    _usedButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    _usedButton.frame=CGRectMake(_canUseButton.right, _canUseButton.top, _canUseButton.width, _canUseButton.height);
    
    [_usedButton setTitle:@"已用红包" forState:UIControlStateNormal];
    
    _usedButton.tag=101;
    
    _usedButton.titleLabel.font=[UIFont systemFontOfSize:15];
    
    [_usedButton setTitleColor:UIColorFromString(@"#b5b5b5") forState:UIControlStateNormal];
    
    [_usedButton addTarget:self action:@selector(ButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_usedButton];
    
    
    
    UIView*lineView2=[[UIView alloc] initWithFrame:CGRectMake(_usedButton.right-0.5, _usedButton.top+3, 1, _usedButton.height-6)];
    
    lineView2.backgroundColor=UIColorFromString(@"#b5b5b5");
    
    [self.view addSubview:lineView2];
    
    
    _UnUseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    _UnUseButton.frame=CGRectMake(_usedButton.right, _usedButton.top, _usedButton.width, _usedButton.height);
    
    [_UnUseButton setTitle:@"过期红包" forState:UIControlStateNormal];
    
    _UnUseButton.tag=102;
    
    _UnUseButton.titleLabel.font=[UIFont systemFontOfSize:15];
    
    [_UnUseButton setTitleColor:UIColorFromString(@"#b5b5b5") forState:UIControlStateNormal];
    
    [_UnUseButton addTarget:self action:@selector(ButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_UnUseButton];
    
    
    
    
    UIView*lineView3=[[UIView alloc] initWithFrame:CGRectMake(_usedButton.right-0.5, _usedButton.top+3, 1, _usedButton.height-6)];
    
    lineView3.backgroundColor=UIColorFromString(@"#b5b5b5");
    
    [self.view addSubview:lineView3];
    
    UIView*view=[[UIView alloc] initWithFrame:CGRectMake(0, _UnUseButton.bottom, SCREEN_WIDTH, 0.5)];
    
    view.backgroundColor=UIColorFromString(@"#b5b5b5");
    
    [self.view addSubview:view];
    
    _slideLineView=[[UIView alloc] initWithFrame:CGRectMake(0, _usedButton.bottom-2, _usedButton.width, 2)];
    
    _slideLineView.backgroundColor=kColor_AppNavColor;
    
    [self.view addSubview:_slideLineView];
    
    
    
}


- (void)creatTableView{
    
    _backGroundScrollView=[[YPBaseScrollView alloc] initWithFrame:CGRectMake(0, _slideLineView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT-64-_slideLineView.bottom)];
    _backGroundScrollView.bounces=NO;
    _backGroundScrollView.pagingEnabled=YES;
    _backGroundScrollView.delegate=self;
    _backGroundScrollView.showsHorizontalScrollIndicator=NO;
    _backGroundScrollView.showsVerticalScrollIndicator=NO;
    _backGroundScrollView.contentSize=CGSizeMake(SCREEN_WIDTH*3, _backGroundScrollView.height);
    [self.view addSubview:_backGroundScrollView];

    
    
    _canUseTableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, _backGroundScrollView.height) style:UITableViewStylePlain];
    _canUseTableView.delegate=self;
    _canUseTableView.dataSource=self;
    _canUseTableView.separatorStyle=0;
    _canUseTableView.tableFooterView=[[UIView alloc] init];
    [_backGroundScrollView addSubview:_canUseTableView];
    
    
    _usedTableView=[[UITableView alloc] initWithFrame:CGRectMake(_canUseTableView.right, 0, SCREEN_WIDTH, _backGroundScrollView.height) style:UITableViewStylePlain];
    _usedTableView.delegate=self;
    _usedTableView.dataSource=self;
    _usedTableView.separatorStyle=0;
    _usedTableView.tableFooterView=[[UIView alloc] init];
    [_backGroundScrollView addSubview:_usedTableView];
    
    
    _UnUseTableView=[[UITableView alloc] initWithFrame:CGRectMake(_usedTableView.right, 0, SCREEN_WIDTH, _backGroundScrollView.height) style:UITableViewStylePlain];
    _UnUseTableView.delegate=self;
    _UnUseTableView.dataSource=self;
    _UnUseTableView.separatorStyle=0;
    _UnUseTableView.tableFooterView=[[UIView alloc] init];
    [_backGroundScrollView addSubview:_UnUseTableView];
    
    
    WeakSelf
    [_canUseTableView addGifHeaderWithRefreshingBlock:^{
        
        [weakSelf getMyRedBag];
    }];

    [_usedTableView addGifHeaderWithRefreshingBlock:^{
        
        [weakSelf getMyRedBag];
    }];
    
    [_UnUseTableView addGifHeaderWithRefreshingBlock:^{
        
        [weakSelf getMyRedBag];
    }];
    
    [_canUseTableView.header beginRefreshing];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
